package org.ktjiaoyu.pojo;

import com.alibaba.fastjson.annotation.JSONField;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Jane
 * @date 2024-06-27 8:36
 */


public class SecurityUser implements UserDetails {

    private SysUser user;

    private List<String> permissions;

    @JSONField(serialize = false)
    private List<GrantedAuthority> authorities ;//=  new ArrayList<>();

    public SecurityUser(SysUser user, List<String> permissions) {
        this.user = user;
        this.permissions = permissions;
    }
//    public SecurityUser(SysUser user, List<GrantedAuthority> authorities) {
//        this.user = user;
//        this.authorities = authorities;
//    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (null ==authorities){
            return authorities;
        }
        authorities =permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        return authorities;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

}